package com.lzw.cpvideoplayer.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.VideoView;

import com.lzw.cpvideoplayer.R;

import com.lzw.cpvideoplayer.data.cHeadData;
import com.lzw.cpvideoplayer.adapter.ContentAdapter;
import com.lzw.cpvideoplayer.mvp.view.LoadDiaLog;
import com.lzw.cpvideoplayer.ulit.OnItemClickLiner;

public class CatalogueFragment extends Fragment {
    private cHeadData mData;
    private RecyclerView mRecyView;
    private VideoView mVideo;
    private TextView mTitle;
    public CatalogueFragment(cHeadData data, VideoView video,TextView title){
        mData = data;
        mVideo = video;
            mTitle = title;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_catalogue,container,false);
        initView(view);
        return view;
    }

    private static final String TAG = "CatalogueFragment";
    private void initView(View view) {
        mRecyView = view.findViewById(R.id.cata_recy_list);
        ContentAdapter adpter = new ContentAdapter(view.getContext(),mData);
        mRecyView.setLayoutManager(new GridLayoutManager(view.getContext(),4));
                adpter.setOnItemListener(new OnItemClickLiner() {
            @Override
            public int OnItemClick(View v, int p) {

//                adpter.notifyDataSetChanged();//刷新所有数据
                int thisPos =  ContentAdapter.currPosition;
                ContentAdapter.currPosition = p;//将当前点击的position传给适配器
                adpter.notifyItemChanged(p);//刷新上一个item
                adpter.notifyItemChanged(thisPos);//刷新上一个item
                mVideo.setVideoPath(mData.getData().getChapterList()[p].getChapterPath());
                LoadDiaLog.getInstance(view.getContext()).show();//显示加载条
                mTitle.setText(mData.getData().getTitle()+"\t"+mData.getData().getChapterList()[p].getTitle());
//                mTextTitle.setText(data.getData().getChapterList()[p].getTitle());
                return p;
            }
        });
        mRecyView.setAdapter(adpter);
    }
}